package com.cloud.common.redis.utils;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Author:  xuanning
 * Date:    2024/5/22 0:32
 */
@Slf4j
public class RedisUtil {

    private static final RedisTemplate redisTemplate=SpringUtil.getBean("redisTemplate");


    public static boolean expire(String key, long time) {
        if (StrUtil.isBlank(key) || time < 0) {
            return false;
        }
        return redisTemplate.expire(key, time, TimeUnit.SECONDS);
    }


    public static boolean expire(String key, long time,TimeUnit timeUnit) {
        if (StrUtil.isBlank(key) || time < 0 || timeUnit==null) {
            return false;
        }
        return redisTemplate.expire(key, time, timeUnit);
    }


    public static boolean expireAt(String key, Date date) {
        if (StrUtil.isBlank(key) || date == null) {
            return false;
        }
        return redisTemplate.expireAt(key, date);
    }


    /**
     * 还有多长时间过期
     */
    public static long ttl(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }


    public static boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }


    public static void delete(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }


    public static boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error("RedisUtil.set error:{}",e);
            return false;
        }
    }


    public static boolean set(String key, Object value,long timeout, TimeUnit unit) {
        try {
            redisTemplate.opsForValue().set(key, value,timeout,unit);
            return true;
        } catch (Exception e) {
            log.error("RedisUtil.set error:{}",e);
            return false;
        }
    }


    public static boolean set(String key, Object value,long timeout) {
        try {
            redisTemplate.opsForValue().set(key, value,timeout,TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            log.error("RedisUtil.set error:{}",e);
            return false;
        }
    }

    public static long incr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            return redisTemplate.opsForValue().increment(key, delta);
        }
    }

    public static long decr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递减因子必须大于0");
        } else {
            return redisTemplate.opsForValue().increment(key, -delta);
        }
    }

}
